/*
 *  sokoban - a Sokoban game for midp-supporting mobile devices
 *  Copyright (C) 2007,2009 Dedi Hirschfeld
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */


package com.xomzom.midlets.sokoban;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

/**
 * The 'select level' dialog.
 * 
 * @author Dedi Hirschfeld
 */
public class SelectLevelDialog
{
    //
    // Members.
    //

    /**
     * The form object for this dialog. will be null if the dialog was not yet
     * initialized.
     */
    private Form m_form;

    /**
     * The previous displayable object - to display when this dialog is hidden.
     */
    private Displayable m_prevView;

    /**
     * The display this form is associated with.
     */
    private Display m_display;
    
    /**
     * The game object this dialog is associated with.
     */
    private Game m_game;

    //
    // Operations.
    //

    /**
     * Create a new SelectLevelDialog, associated with the given display.
     * @param game The sokoban game object to associate this dialog with.
     */
    public SelectLevelDialog(Game game)
    {
        m_game = game;
        m_display = Display.getDisplay(game);
    }

    /**
     * Show the dialog.
     */
    public void show()
    {
        if (m_form == null)
        {
            m_form = new Form("Select level:");
            init(m_form);
        }
        m_prevView = m_display.getCurrent();
        m_display.setCurrent(m_form);
    }

    /**
     * Initialize the dialog. called once when the dialog is first shown. 
     * @param form The dialog form. Widgets should be added to this form. 
     */
    public void init(Form form)
    {
        final Command cmdOk = new Command("OK", Command.OK, 1);
        final Command cmdCancel = new Command("Cancel", Command.CANCEL, 2);
        final TextField textField = new TextField("Select level:", "",
                5, TextField.NUMERIC);
        textField.setConstraints(TextField.NUMERIC);

        form.addCommand(cmdOk);
        form.addCommand(cmdCancel);
        form.append(textField);
        form.setCommandListener(new CommandListener() {
            public void commandAction(Command c, Displayable d)
            {
                if (c == cmdOk)
                {
                    String levelAsString = textField.getString();
                    try
                    {
                        int level = Integer.parseInt(levelAsString);
                        m_game.setLevel(level, false);
                    }
                    catch(NumberFormatException e)
                    {
                        System.err.println("Hey! Field allows only nums!");
                    }
                    m_display.setCurrent(m_prevView);
                }
                else if (c == cmdCancel)
                {
                    m_display.setCurrent(m_prevView);
                }
            }
        });
    }
}